/**
 * Copyright (C) 2010 David Simonek <dafesimonek@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.dafe.partyband.gui.songs;

import java.util.Map;
import javax.swing.DefaultComboBoxModel;
import javax.swing.RowFilter;
import javax.swing.event.ChangeListener;
import javax.swing.table.TableModel;

/**
 *
 * @author dafe
 */
public class ViewConfigurationPanel extends javax.swing.JPanel {
    private final Map<String, RowFilter<TableModel, Integer>> filtersMap;
    private final ChangeListener settingsL;

    /** Creates new form ViewConfigurationPanel */
    public ViewConfigurationPanel(final Settings settings, Map<String, RowFilter<TableModel, Integer>> filtersMap, ChangeListener settingsL) {
        initComponents();
        this.filtersMap = filtersMap;
        songFilter.setModel(new DefaultComboBoxModel(filtersMap.keySet().toArray()));
        showColors.setSelected(settings.isShowColors);
        showIcons.setSelected(settings.isShowIcons);
        songFilter.setSelectedItem(settings.filterName);
        // last to not fire settings change here from constructor
        this.settingsL = settingsL;
    }

    public static final class Settings {
        boolean isShowIcons;
        boolean isShowColors;
        RowFilter<TableModel, Integer> filter;
        String filterName;
    }

    public Settings getSettings () {
        Settings settings = new Settings();
        settings.isShowColors = showColors.isSelected();
        settings.isShowIcons = showIcons.isSelected();
        String filterName = (String)songFilter.getSelectedItem();
        settings.filter = filtersMap.get(filterName);
        settings.filterName = filterName;
        return settings;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        songFilter = new javax.swing.JComboBox();
        showColors = new javax.swing.JCheckBox();
        showIcons = new javax.swing.JCheckBox();

        jLabel1.setText(org.openide.util.NbBundle.getMessage(ViewConfigurationPanel.class, "ViewConfigurationPanel.jLabel1.text",new Object[] {})); // NOI18N

        songFilter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                songFilterActionPerformed(evt);
            }
        });

        showColors.setText(org.openide.util.NbBundle.getMessage(ViewConfigurationPanel.class, "ViewConfigurationPanel.showColors.text",new Object[] {})); // NOI18N
        showColors.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showColorsActionPerformed(evt);
            }
        });

        showIcons.setText(org.openide.util.NbBundle.getMessage(ViewConfigurationPanel.class, "ViewConfigurationPanel.showIcons.text",new Object[] {})); // NOI18N
        showIcons.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showIconsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(songFilter, 0, 198, Short.MAX_VALUE)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
            .addComponent(showColors)
            .addComponent(showIcons)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(songFilter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(showColors)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(showIcons))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void showIconsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showIconsActionPerformed
        fireSettingsChange();
    }//GEN-LAST:event_showIconsActionPerformed

    private void showColorsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showColorsActionPerformed
        fireSettingsChange();
    }//GEN-LAST:event_showColorsActionPerformed

    private void songFilterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_songFilterActionPerformed
        fireSettingsChange();
    }//GEN-LAST:event_songFilterActionPerformed

    void fireSettingsChange () {
        if (settingsL != null) {
            settingsL.stateChanged(null);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JCheckBox showColors;
    private javax.swing.JCheckBox showIcons;
    private javax.swing.JComboBox songFilter;
    // End of variables declaration//GEN-END:variables

}
